package com.rc.evcharger.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateHelper {
    /**
     * 通用日期格式
     */
    public static final String DATE_COMMON_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_FORMAT_YYYYMMDDHHMM = "yyyyMMddHHmm";

    public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";

    public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";

    public static final String DATE_FORMAT_HHMMSS = "HH:mm:ss";

    public static final String DATE_FORMAT_HHMM = "HH:mm";

    public static final String DATE_FORMAT_YYMMDDHHMMSS = "yyMMddHHmmss";

    public static final String DATE_FORMAT_YYMMDDHHMMSS_SSS = "yyMMddHHmmssSSS";

    public static final String DATE_FORMAT_HH = "HH";
    public static final String DATE_FORMAT_MM = "mm";

    public static Date getCurrentDate() {
        Calendar calender = Calendar.getInstance();
        return calender.getTime();
    }

    /**
     * 获取一个日期，该日期是在date的days天之前(days是负数)或之后(days是正数)
     *
     * @param date
     * @param days
     * @return Date
     */
    public static Date getDateByCalculateDays(Date date, int days) {
        Calendar calender = Calendar.getInstance();
        calender.setTime(date);
        calender.add(Calendar.DATE, days);
        return calender.getTime();
    }

    /**
     * 获取一个日期，该日期是在date的seconds秒之前(seconds是负数)或之后(seconds是正数)
     *
     * @param date
     * @return Date
     */
    public static Date getDateByCalculateSeconds(Date date, int seconds) {
        Calendar calender = Calendar.getInstance();
        calender.setTime(date);
        calender.add(Calendar.SECOND, seconds);
        return calender.getTime();
    }

    /**
     * 把日期按照某个格式转换成字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String dataToString(Object date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }

    /**
     * 把字符串按照某个格式转换成日期类型
     *
     * @param strDate
     * @param format
     * @return
     */
    public static Date stringToDate(String strDate, String format) {
        DateFormat df = new SimpleDateFormat(format);
        Date date = null;
        if (strDate != null && !"".equals(strDate)) {
            try {
                date = df.parse((String) strDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    public static String getRandomNum() {
        StringBuffer sb = new StringBuffer();
        sb.append(dataToString(new Date(), DATE_FORMAT_YYYYMMDDHHMM));
        sb.append(String.valueOf(Math.round(Math.random() * 8999 + 1000)));
        return sb.toString();
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    public static int minuteBetween(Date smDate, Date bdate) throws ParseException {
        return (int) (bdate.getTime() - smDate.getTime()) / 1000 / 60;
    }


    /**
     * 获取当前时间的BCD格式
     *
     * @return
     */
    public static String getBCDTime() {
        StringBuffer sb = new StringBuffer();
        Calendar instance = Calendar.getInstance();

        //年
        String year = ((instance.get(Calendar.YEAR)) + "");
        sb.append(year.substring(0, 2)).append(DataUtil.SPLIT)
                .append(year.substring(2)).append(DataUtil.SPLIT);
        //sb.append("20 20 ");

        //月
        String month = (instance.get(Calendar.MONTH) + 1) + "";
        if (month.length() == 1) {
            sb.append("0").append(month).append(DataUtil.SPLIT);
        } else {
            sb.append(month).append(DataUtil.SPLIT);
        }

        //日
        String day = instance.get(Calendar.DAY_OF_MONTH) + "";
        if (day.length() == 1) {
            sb.append("0").append(day).append(DataUtil.SPLIT);
        } else {
            sb.append(day).append(DataUtil.SPLIT);
        }

        //时
        String hour = instance.get(Calendar.HOUR_OF_DAY) + "";
        if (hour.length() == 1) {
            sb.append("0").append(hour).append(DataUtil.SPLIT);
        } else {
            sb.append(hour).append(DataUtil.SPLIT);
        }

        //分
        String min = instance.get(Calendar.MINUTE) + "";
        if (min.length() == 1) {
            sb.append("0").append(min).append(DataUtil.SPLIT);
        } else {
            sb.append(min).append(DataUtil.SPLIT);
        }

        //秒
        String sed = instance.get(Calendar.SECOND) + "";
        if (sed.length() == 1) {
            sb.append("0").append(sed).append(DataUtil.SPLIT);
        } else {
            sb.append(sed).append(DataUtil.SPLIT);
        }
        return sb.toString().trim();
    }

    /**
     * 添加一秒
     *
     * @param date
     * @param second
     * @return
     */
    public static Date addSecond(Date date, int second) {

        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.add(Calendar.SECOND, second);
        return instance.getTime();
    }

    /**
     * @param date
     * @param second
     * @return
     */
    public static Date splitDateAndTime(Date sourceDate, Date targetDate) {
        String date = DateHelper.dataToString(sourceDate, DateHelper.DATE_FORMAT_YYYY_MM_DD);
        String time = DateHelper.dataToString(targetDate, DateHelper.DATE_FORMAT_HHMMSS);
        String dateTime = date + " " + time;
        return DateHelper.stringToDate(dateTime, DateHelper.DATE_COMMON_FORMAT);
    }

    /**
     * 比较目标时段是否在指定时段区间内
     * @param beginTime
     * @param endTime
     * @param targerDate
     * @return
     * @throws ParseException
     */
    public static boolean compareTargerDate(Date beginTime, Date endTime, Date targerDate) throws ParseException {
        DateFormat df = new SimpleDateFormat(DateHelper.DATE_FORMAT_HHMMSS);

        String beginTimeStr = df.format(beginTime);
        String endTimeStr = df.format(endTime);
        String targerDateStr = df.format(targerDate);

        Date beginTime0 = df.parse(beginTimeStr);
        Date endTime0 = df.parse(endTimeStr);
        Date targerDate0 = df.parse(targerDateStr);
        if (targerDate0.getTime() >= beginTime0.getTime() && targerDate0.getTime() <= endTime0.getTime()) {
            return true;
        }
        return false;

    }


    public static void main(String[] args) throws ParseException {
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //Date d1 = sdf.parse("2017-05-01 10:10:10");
        //Date d2 = sdf.parse("2017-05-15 00:00:00");
        //System.out.println(daysBetween(d1, d2));

        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.HOUR_OF_DAY, 22);


        Date date1 = DateHelper.splitDateAndTime(new Date(), instance.getTime());
        System.out.println(DateHelper.dataToString(date1, DateHelper.DATE_COMMON_FORMAT));
    }

}
